
goog.provide('esn.Application');

goog.require('esn.AppContext');
goog.require('esn.CommunityDormSurvey');
goog.require('esn.CommunityFloorSurvey');
goog.require('esn.EnvironmentSurvey');
goog.require('esn.NetworkCreator');
goog.require('esn.NetworkSurvey');
goog.require('esn.PersonalSurvey');
goog.require('esn.ProgressBar');
goog.require('esn.RandomFriendsSurvey');
goog.require('esn.SurveyIntro');
goog.require('esn.StrainedNetworkSurvey');
goog.require('esn.constants');
goog.require('esn.model.StudentModel');
goog.require('esn.client.StudentClient');
goog.require('esn.client.SurveyClient');
goog.require('esn.soy.completedSurvey');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.json');
goog.require('goog.style');
goog.require('goog.ui.Component');
goog.require('goog.ui.CustomButton');


/**
 * @constructor
 */
esn.Application = function(userJson, studentsJson, isDevMode, opt_domHelper) {
  goog.base(this, opt_domHelper);

  this.user_ = new esn.model.StudentModel(userJson);
  this.appContext_ = new esn.AppContext(this.user_, isDevMode);
  this.registerServices_();

  this.student_client_ = this.appContext_.getService(
      esn.constants.ServiceId.STUDENT_CLIENT);

  this.students_ = goog.array.map(studentsJson, function(studentJson) {
    return new esn.model.StudentModel(studentJson);
  });
};
goog.inherits(esn.Application, goog.ui.Component);

esn.Application.prototype.progressBar_;

esn.Application.prototype.studyIntro_;

esn.Application.prototype.personalSurvey_;

esn.Application.prototype.communityFloorSurvey_;

esn.Application.prototype.environmentSurvey_;

esn.Application.prototype.networkCreator_;

esn.Application.prototype.networkSurvey_;

esn.Application.prototype.strainedNetworkSurvey_;

esn.Application.prototype.randomFriendsSurvey_;

esn.Application.prototype.friendSurveys_;

esn.Application.prototype.registerServices_ = function() {
  var xhrManager = this.appContext_.getXhrManager();

  var studentService = new esn.client.StudentClient(xhrManager);
  this.appContext_.registerService(esn.constants.ServiceId.STUDENT_CLIENT,
      studentService);

  var surveyService = new esn.client.SurveyClient(xhrManager);
  this.appContext_.registerService(esn.constants.ServiceId.SURVEY_CLIENT,
      surveyService);
};

esn.Application.prototype.createDom = function() {
  goog.base(this, 'createDom');
  this.progressBar_ = new esn.ProgressBar();
  this.progressBar_.render(goog.dom.getElement('progressBarContainer'));

  this.studyIntro_ = new esn.SurveyIntro(this.appContext_);
  this.addChild(this.studyIntro_);

  // Create the other surveys but don't render them.
  this.personalSurvey_ = new esn.PersonalSurvey(this.appContext_);
  this.communityFloorSurvey_ = new esn.CommunityFloorSurvey(this.appContext_);
  this.environmentSurvey_ = new esn.EnvironmentSurvey(this.appContext_);
  this.networkCreator_ = new esn.NetworkCreator(this.appContext_,
      this.students_);
  this.networkSurvey_ = new esn.NetworkSurvey(this.appContext_);
  this.strainedNetworkSurvey_ = new esn.StrainedNetworkSurvey(
      this.appContext_, this.students_);
  this.randomFriendsSurvey_ = new esn.RandomFriendsSurvey(this.appContext_);

  this.addChild(this.personalSurvey_);
  this.addChild(this.communityFloorSurvey_);
  this.addChild(this.environmentSurvey_);
  this.addChild(this.networkCreator_);
  this.addChild(this.networkSurvey_);
  this.addChild(this.strainedNetworkSurvey_);
  this.addChild(this.randomFriendsSurvey_);

  // If the user has completed the survey jump to the completed page.
  if (this.user_.getStatus() == 'completed' && !this.user_.isAdmin()) {
    this.displayCompletedSurvey_();
  } else {
    this.studyIntro_.render(this.getElement());
  }
};

esn.Application.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  // Listen for survey changes.
  this.getHandler().listen(
      this,
      esn.BaseSurvey.EventType.CHANGE,
      this.handleSurveyChange_);

  this.setContinueEvents_();
  this.setBackEvents_();
};

esn.Application.prototype.setContinueEvents_ = function() {
  this.getHandler().listen(
      this.studyIntro_,
      esn.SurveyIntro.EventType.CONTINUE,
      this.handleSurveyInfoContinue_);

  this.getHandler().listen(
      this.personalSurvey_,
      esn.PersonalSurvey.EventType.CONTINUE,
      this.handlePersonalSurveyContinue_);

  this.getHandler().listen(
      this.communityFloorSurvey_,
      esn.CommunityFloorSurvey.EventType.CONTINUE,
      this.handleCommunityFloorSurveyContinue_);

  this.getHandler().listen(
      this.environmentSurvey_,
      esn.EnvironmentSurvey.EventType.CONTINUE,
      this.handleEnvironmentSurveyContinue_);

  this.getHandler().listen(
      this.networkCreator_,
      esn.NetworkCreator.EventType.CONTINUE,
      this.handleNetworkCreatorContinue_);

  this.getHandler().listen(
      this.networkSurvey_,
      esn.NetworkSurvey.EventType.CONTINUE,
      this.handleNetworkSurveyContinue_);

  this.getHandler().listen(
      this.strainedNetworkSurvey_,
      esn.StrainedNetworkSurvey.EventType.CONTINUE,
      this.handleStrainedNetworkSurveyContinue_);

  this.getHandler().listen(
      this.randomFriendsSurvey_,
      esn.RandomFriendsSurvey.EventType.CONTINUE,
      this.handleRandomFriendsSurveyContinue_);
};

esn.Application.prototype.setBackEvents_ = function() {
  this.getHandler().listen(
      this.personalSurvey_,
      esn.PersonalSurvey.EventType.BACK,
      this.handlePersonalSurveyBack_);

  this.getHandler().listen(
      this.communityFloorSurvey_,
      esn.CommunityFloorSurvey.EventType.BACK,
      this.handleCommunityFloorSurveyBack_);

  this.getHandler().listen(
      this.environmentSurvey_,
      esn.EnvironmentSurvey.EventType.BACK,
      this.handleEnvironmentSurveyBack_);

  this.getHandler().listen(
      this.networkCreator_,
      esn.NetworkCreator.EventType.BACK,
      this.handleNetworkCreatorBack_);

  this.getHandler().listen(
      this.networkSurvey_,
      esn.NetworkSurvey.EventType.BACK,
      this.handleNetworkSurveyBack_);

  this.getHandler().listen(
      this.strainedNetworkSurvey_,
      esn.StrainedNetworkSurvey.EventType.BACK,
      this.handleStrainedNetworkSurveyBack_);

  this.getHandler().listen(
      this.randomFriendsSurvey_,
      esn.RandomFriendsSurvey.EventType.BACK,
      this.handleRandomFriendsSurveyBack_);
};

esn.Application.prototype.handleSurveyChange_ = function(e) {
  this.progressBar_.updatePercent(e.getSectionId(), e.getPercentCompleted());
};

//---------- CONTINUE button handlers ----------

esn.Application.prototype.handleSurveyInfoContinue_ = function(e) {
  if (this.user_.startedPreviousSession()) {
    this.progressBar_.updatePercent(esn.PersonalSurvey.SECTION_ID, 100);
    this.displaySurvey_(this.communityFloorSurvey_);
  } else {
    this.displaySurvey_(this.personalSurvey_);
  }
  this.progressBar_.show();
};

esn.Application.prototype.handlePersonalSurveyContinue_ = function(e) {
  this.displaySurvey_(this.communityFloorSurvey_);
};

esn.Application.prototype.handleCommunityFloorSurveyContinue_ = function(e) {
  this.displaySurvey_(this.environmentSurvey_);
};

esn.Application.prototype.handleEnvironmentSurveyContinue_ = function(e) {
  this.displaySurvey_(this.networkCreator_);
};

esn.Application.prototype.handleNetworkCreatorContinue_ = function(e) {
  this.friendSurveys_ = e.target.getFriendSurveys();
  this.networkSurvey_.setFriendSurveys(this.friendSurveys_);
  this.displaySurvey_(this.networkSurvey_);
};

esn.Application.prototype.handleNetworkSurveyContinue_ = function(e) {
  this.randomFriendsSurvey_.setFriendSurveys(this.friendSurveys_);
  this.displaySurvey_(this.randomFriendsSurvey_);
};

esn.Application.prototype.handleStrainedNetworkSurveyContinue_ = function(e) {
  this.randomFriendsSurvey_.setFriendSurveys(this.friendSurveys_);
  this.displaySurvey_(this.randomFriendsSurvey_);
};

esn.Application.prototype.handleRandomFriendsSurveyContinue_ = function(e) {
  this.displayCompletedSurvey_();
};

//---------- BACK button handlers ----------

esn.Application.prototype.handlePersonalSurveyBack_ = function(e) {
  this.displaySurvey_(this.studyIntro_);
};

esn.Application.prototype.handleCommunityFloorSurveyBack_ = function(e) {
  if (this.user_.startedPreviousSession()) {
    this.displaySurvey_(this.studyIntro_);
  } else {
    this.displaySurvey_(this.personalSurvey_);
  }
};


esn.Application.prototype.handleEnvironmentSurveyBack_ = function(e) {
  this.displaySurvey_(this.communityFloorSurvey_);
};

esn.Application.prototype.handleNetworkCreatorBack_ = function(e) {
  this.displaySurvey_(this.environmentSurvey_);
};

esn.Application.prototype.handleNetworkSurveyBack_ = function(e) {
  this.displaySurvey_(this.networkCreator_);
};

esn.Application.prototype.handleStrainedNetworkSurveyBack_ = function(e) {
  this.displaySurvey_(this.networkSurvey_);
};

esn.Application.prototype.handleRandomFriendsSurveyBack_ = function(e) {
  this.displaySurvey_(this.networkSurvey_);
};

//------------------------------------------

esn.Application.prototype.displaySurvey_ = function(survey) {
  goog.dom.removeChildren(this.getElement());
  if (!survey.getElement()) {
    survey.render(this.getElement());
  } else {
    goog.dom.appendChild(this.getElement(), survey.getElement());
  }

  // Start at the top of the page.
  var appContainerEl = goog.dom.getElement('applicationContainer');
  if (appContainerEl) {
    appContainerEl.scrollIntoView(true);
  }

  this.saveUserStatus(survey.getSectionId());
};

esn.Application.prototype.displayCompletedSurvey_ = function() {
  var completedSurveyEl = soy.renderAsElement(esn.soy.completedSurvey.content);
  goog.dom.removeChildren(this.getElement());
  goog.dom.appendChild(this.getElement(), completedSurveyEl);
  goog.dom.getElement('applicationContainer').scrollIntoView(true);

  // Save the user's status as "completed".
  this.saveUserStatus('completed');
};

esn.Application.prototype.saveUserStatus = function(sectionId) {
  this.student_client_.saveStudentStatus(sectionId);
  // Update the user's status locally.
  this.user_.setStatus(sectionId);
};


goog.exportSymbol('esn.Application', esn.Application);
goog.exportProperty(esn.Application.prototype, 'render',
    esn.Application.prototype.render);

goog.exportSymbol('goog.ui.CustomButton', goog.ui.CustomButton);
goog.exportProperty(goog.ui.CustomButton.prototype, 'decorate',
    goog.ui.CustomButton.prototype.decorate);

goog.exportSymbol('goog.events.listenOnce', goog.events.listenOnce);
goog.exportSymbol('goog.json.serialize', goog.json.serialize);
goog.exportSymbol('goog.ui.Component.EventType.ACTION',
    goog.ui.Component.EventType.ACTION);
